<template>
	<view>

		<!-- <view class=""></view> -->
		<view v-if="startJc" style="padding-top: 100rpx;">
			<camera style="width: 80vw; height: 80vw;  margin: 0 auto;border-radius: 50%;" v-if="!src" class="camera"
				device-position="back" frame-size="small" flash="off" binderror="error"></camera>
			<image style="width: 480rpx; height: 480rpx;  margin: 0 auto; display: block;" mode="aspectFill"
				class="icon" :src="src" v-if="src"></image>
			<canvas class="canvas" :style="'height:'+windowHeight+'px;margin-top:'+windowHeight+'px;'"
				canvas-id="firstCanvas"></canvas>
		</view>

		<view v-if="!startJc" style="padding: 50px 10vw;">
			<image style="width: 80vw;height: 80vw;" src="../../static/renliantu.png" mode=""></image>
		</view>
		<view v-if="!startJc" class="luru-btn" @tap="inputFace"> {{is_faceid_title}}</view>
		<view v-if="!startJc" class="luru-text">点击验证即表示您已阅读并同意《用户隐私协议》</view>

	</view>
</template>
<style lang="scss">
	page {
		background-color: #FFFFFF;
	}

	.title {
		font-size: 32rpx;
		text-align: center;
		display: block;
		margin: 100rpx 0;
	}

	.camera {
		width: 400rpx;
		height: 400rpx;
		z-index: 1;
	}

	.canvas {
		width: 100%;
		box-sizing: border-box;
		z-index: 0;
		position: fixed;
		bottom: 1000px;
	}

	.luru-btn {
		width: 80vw;
		height: 40px;
		background: #2e8bf0;
		border-radius: 20px;
		line-height: 40px;
		text-align: center;
		color: #fff;
		font-size: 14px;
		margin: 0 auto;
	}

	.luru-text {
		text-align: center;
		color: #ccc;
		line-height: 50px;
		font-size: 14px;

	}
</style>
<script>
	import {baseUrl} from '@/utils/config.js'
	const app = getApp()
	let listener = ''
	export default {
		data() {
			return {
				id: '',
				src: "",
				status: {
					record: false,
					camera: 'back'
				},
				Bullet_frame: false,
				serintervalTime: null,
				device: true,
				speedMaxCount: 60,
				windowHeight: 500,
				goodsId: "",
				h_title: "销售人员认证",
				startJc: false,
				livePusher: null,
				onOff: false,
				longitude: null,
				latitude: null,
				face_imgs: '',
				is_fool:'',
				is_faceid_title:'上传信息',

			}
		},
		onLoad(query) {
			this.id = query.id
			this.finalss();
		},

		methods: {
			async finalss() {
				await this.request('index/is_fool', {
				}, 'POST').then(res => {
					this.is_fool = res.data.is_fool
					this.is_faceid_title = res.data.is_faceid_title
				})
			},
			inputFace() {
				const _this = this
				// #ifdef MP-WEIXIN
				wx.getLocation({
					type: 'wgs84',
					success(res) {
						const latitude = res.latitude
						const longitude = res.longitude
						_this.longitude = longitude
						_this.latitude = latitude
						
					}
				})
				// #endif
				const ctx = wx.createCameraContext()
				this.startJc = true
				this.startTacking()
			},
			startTacking() {
				var _that = this;
				var count = 0;
				const context = wx.createCameraContext();
				if (!context.onCameraFrame) {
					var message = '基础库 2.7.0 开始支持".';
					uni.showToast({
						title: message,
						icon: 'none'
					});
					return;
				}
				listener = context.onCameraFrame(function(res) { // 每秒60帧，这里控制每0.5获取一次图片 
					if (count < _that.speedMaxCount) {
						count++;
						return;
					}
					count = 0;
					_that.jschangeDataToBase64(res)
					console.log('00001')
				});
				// start
				listener.start();

			},

			jschangeDataToBase64(frame) {
				var data = new Uint8Array(frame.data);
				var clamped = new Uint8ClampedArray(data);
				let that = this
				var contex = uni.createCanvasContext('firstCanvas')
				uni.canvasPutImageData({
					canvasId: 'firstCanvas',
					x: 0,
					y: 0,
					width: frame.width,
					height: frame.height,
					data: clamped,
					success(res) {
						uni.canvasToTempFilePath({
							x: 0,
							y: 0,
							width: frame.width,
							height: frame.height,
							canvasId: 'firstCanvas',
							fileType: 'png',
							destWidth: frame.width,
							destHeight: frame.height,
							quality: 0.8,
							success(res) {
								let oddurl = res.tempFilePath
								console.log('0002')
								that.detectLiveFace(res.tempFilePath)
							}
						}, that)
					},
					fail(res) {
						console.log('调用失败', res)
					}
				})
			},
			// 结束相机实时帧
			stopTacking() {
				if (listener) {
					listener.stop();
				}
			},
			// 开始相机实时帧
			startcamera: function() {
				if (listener) {
					listener.start()
				}
			},
			detectLiveFace(url) {
				
				const that = this;
				if(!that.onOff){
					that.onOff = true
					uni.uploadFile({
						url: `${baseUrl}goods/detection`,
						filePath: url,
						name: 'file',
						header: {
							'token': uni.getStorageSync('usertoken'),
						},
						success: (u) => {
							const res = JSON.parse(u.data) 
							that.onOff = false
							if (res.code == 1) {
								that.face_imgs = res.data.img
								that.finals()
							} else {
								uni.showToast({
									icon: 'none',
									title: '请上传人脸照片',
									duration: 500
								})
							}
					
						}
					});
				}
				


			},
			async finals() {
				const that = this;
				that.onOff = true
				await that.request('goods/identify', {
					id: that.id,
					face_imgs: that.face_imgs,
					longitude: this.longitude,
					latitude: this.latitude
				}, 'POST').then(res => {
					console.log("1111",res.data);
					if(res.data.mp3 != ''){
						// 创建innerAudioContext对象
						const innerAudioContext = uni.createInnerAudioContext();
						// 开始自动播放
						innerAudioContext.autoplay = true;
						// 设置音频地址
						innerAudioContext.src = res.data.mp3;
						// 开始播放的回调函数
						innerAudioContext.onPlay(() => {
						  console.log('开始播放');
						});
						
						// 播放报错的事件监听
						innerAudioContext.onError((res) => {
						  console.log(res.errMsg);
						  console.log(res.errCode);
						});
					}
					
					if (res.code == 1) {
						uni.navigateTo({
							url: `/pages/authStatus/authStatus?status=1`
						})
					}
					if (res.code == 0) {
						uni.navigateTo({
							url: `/pages/authStatus/authStatus?status=0`
						})
					}
					if (res.code == 2) {
						uni.navigateTo({
							url: `/pages/authStatus/authStatus?status=2`
						})
					}
				})
			},


			setFace() {
				uni.showLoading();
				const ctx = uni.createCameraContext();
				ctx.takePhoto({
					quality: 'high',
					success: (res) => {
						this.$u.api.uploadFile({
							url: 'user/upload/one?app=saleface',
							filePath: res.tempImagePath,
							name: 'file',
							success: uploadRes => {
								if (uploadRes.code == 0) {
									this.$u.toast(res.msg)
								} else {

									this.$u.http.get("order/order/update", {
										order_sn: this.order_sn,
										entity_id: uploadRes.data.entityId
									}).then(res => {
										uni.navigateTo({
											url: "/pages/order/confirm?order_sn=" +
												this.order_sn
										})
										this.face_image = uploadRes.data.preview_url;
									})
								}
							},
							complete: res => {
								uni.hideLoading();
							}
						});
					}
				});

			}
		}
	}
</script>

<style>

</style>